﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PictureTool;

using Aspose.Words;
using Aspose.Words.Tables;
using Aspose.Words.Fields;
using Aspose.Words.Layout;
using Aspose.Words.Drawing;
using System.Collections;
namespace WordTool
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void btnGetPageCount_Click(object sender, EventArgs e)
        {
            try
            {
                string[] files = System.IO.Directory.GetFiles(Config.OutPutPath);
                tBoxPageCount.Text = Convert.ToString(files.Length);
            }
            catch (Exception ex)
            {
                MessageBox.Show("目录没有文件"+ex.Message);
            }
        }

        private void btnCreateWord_Click(object sender, EventArgs e)
        {
            try
            {
                Document doc = new Document();
                DocumentBuilder builder = new DocumentBuilder(doc);
                for (int i = 0; i < Convert.ToInt32(tBoxPageCount.Text); i++)
                {
                   // builder.InsertBreak(BreakType.PageBreak);//分页    
                    Shape shape = builder.InsertImage(Config.OutPutPath + "\\" + i + ".jpg", 1920, 400);
                }

                doc.Save(Config.PicturePath + "123.docx");
            }
            catch (Exception ex)
            {
                MessageBox.Show("创建失败" + ex.Message);
            }
           
        }

        private void btnAddImg_Click(object sender, EventArgs e)
        {
            Document doc = new Document(Config.PicturePath + "123.docx");
            DocumentBuilder builder = new DocumentBuilder(doc);
           //// LayoutCollector layoutCollector = new LayoutCollector(doc);
           // IEnumerator enumerator = doc.SelectNodes("//Body/Paragraph").GetEnumerator();
           // for (int page = 1; page <= doc.PageCount; page++)
           // {
           //     while (enumerator.MoveNext())
           //     {
           //         Paragraph paragraph = (Paragraph)enumerator.Current;
           //         if (layoutCollector.GetStartPageIndex(paragraph) == page)
           //         {
           //             AddImageToPage(paragraph, page);
           //             break;
           //         }
           //     }
           // }
            for (int page = 0; page <= 58; page++)
            {
               
                Shape shape = builder.InsertImage(Config.OutPutPath + "\\" + page + ".jpg", 1920,400 );
            }
                doc.UpdatePageLayout();

            doc.Save(Config.PicturePath + "TestFile Out.docx");
        }
        public static void AddImageToPage(Paragraph para, int page)
        {
            Document doc = (Document)para.Document;

            DocumentBuilder builder = new DocumentBuilder(doc);
            builder.MoveTo(para);
            // Add a logo to the top left of the page. The image is placed infront of all other text.
            Shape shape = builder.InsertImage(Config.OutPutPath+"\\"+page+".jpg", RelativeHorizontalPosition.Page, 60,
                RelativeVerticalPosition.Page, 60, -1, -1, WrapType.None);

          
        }
    }
}
